package com.example.video;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    Handler handler;
    List<Data> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.obj.equals("100")){
                    listView = findViewById(R.id.lv);
                    listView.setAdapter(new MyAdapter());
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this, MainActivity2.class);
                            intent.putExtra("link", list.get(position).getLink());
                            startActivity(intent);
                        }
                    });
                }
            }
        };
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL("https://api.apiopen.top/api/getMiniVideo");
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    if(coon.getResponseCode()==200){
                        InputStream input = coon.getInputStream();
                        InputStreamReader reader = new InputStreamReader(input);
                        BufferedReader br = new BufferedReader(reader);
                        StringBuffer sbf = new StringBuffer();
                        String str = null;
                        while ((str = br.readLine())!=null){
                            sbf.append(str);
                        }
                        str = sbf.toString();
                        JSONObject jsonObject = new JSONObject(str);
                        if (jsonObject.getInt("code")==200){
                            JSONObject jsonObject1 = jsonObject.getJSONObject("result");
                            JSONArray jsonArray = jsonObject1.getJSONArray("list");
                            for (int i=0;i<jsonArray.length();i++){
                                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                                String title = jsonObject2.getString("title");
                                String link = jsonObject2.getString("playurl");
                                String image = jsonObject2.getString("picurl");
                                list.add(new Data(title, link, image));
                            }
                        }
                        System.out.println(list);
                        Message message = new Message();
                        message.obj = "100";
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this, R.layout.activity_item, null);
            TextView title = view.findViewById(R.id.textView2);
            ImageView imageView = view.findViewById(R.id.image);
            title.setText(list.get(position).getTitle());
            getImage(list.get(position).getImage(), imageView);
            return view;
        }
    }
    public void getImage(String u, ImageView imageView){
        Handler handler1 = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                imageView.setImageBitmap((Bitmap) msg.obj);
            }
        };
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL(u);
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    if(coon.getResponseCode()==200){
                        InputStream inputStream = coon.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        Message message = new Message();
                        message.obj = bitmap;
                        handler1.sendMessage(message);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

            }
        }.start();
    }
}